﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using LanguagePolish.EntityModel;

namespace LanguagePolish.Views
{
    /// <summary>
    /// Interaction logic for LoginView.xaml
    /// </summary>
    public partial class LoginView : Window
    {
        public LoginView()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            preloadgif.Visibility = Visibility.Visible;
            var context = LinksProvider.Model.EntityContext;
            //подумать, как запоминать пароли
            string password = Crypt.Encrypt(this.Pass.Password,"pshe");
            //password = password.GetHashCode().ToString();
            User user = context.Users.FirstOrDefault(x => x.UserLogin == UserLogin.Text && x.UserPassword == password);
            if (user != null)
            {
                preloadgif.Visibility = Visibility.Visible;
                #region saving password
                if ((bool)cbRemember.IsChecked) 
                {
                    Microsoft.Win32.RegistryKey rkey;
                    rkey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("TeacherPassword");
                    rkey.SetValue("Name", UserLogin.Text);
                    rkey.SetValue("Pass", password);
                    rkey.Close();
                }
                #endregion
                LinksProvider.Model.SetUserPermissions(user);
                MainWindow mw = new MainWindow();
                mw.Show();
                this.Close();
            }
            else 
            {
                this.Pass.Password = "";
                MessageBox.Show("Incorrect Username or Password!");
                preloadgif.Visibility = Visibility.Hidden;
            }
        }
    }
}
